package com.youan.serve.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youan.pangu.base.exception.BusinessException;
import com.youan.serve.dao.domain.Role;
import com.youan.serve.dao.domain.UserRole;
import com.youan.serve.dao.idao.RoleMapper;
import com.youan.serve.service.IRoleService;
import com.youan.serve.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author auto_code
 * @since 2020-03-04
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private IUserRoleService iUserRoleService;

    @Override
    public boolean addJudgeRole(Role role) throws Exception {
        if (role == null) {
            throw new BusinessException("对象为空！");
        }
        if (role.getRoleName() == null) {
            throw new BusinessException("角色名称为空！");
        }
        return true;
    }

    @Override
    public boolean deleteJudgeRole(String roleId) throws Exception {
        if(roleId==null || roleId.equals("")){
            throw new BusinessException("删除角色时传入的角色id不存在！");
        }
        List<UserRole> userRoleList = iUserRoleService.list(new LambdaUpdateWrapper<UserRole>().eq(UserRole::getRoleId, roleId));
        if(userRoleList.size()>0){
            throw new BusinessException("该角色已被赋予用户，不允许删除！");
        }
        return true;
    }
}
